home *** CD-ROM | disk | FTP | other *** search
- Path: news1.h1.usa.pipeline.com!usenet
- From: grantp@usa.pipeline.com(Pete)
- Newsgroups: comp.lang.c++
- Subject: Re: HELP! cin.getline to cin
- Date: 23 Feb 1996 00:33:19 GMT
- Organization: Kalevi, Inc.
- Message-ID: <4gj20f$ii0@news1.usa.pipeline.com>
- NNTP-Posting-Host: pipe4.h1.usa.pipeline.com
- X-PipeUser: grantp
- X-PipeHub: usa.pipeline.com
- X-PipeGCOS: (Pete)
- X-Newsreader: Pipeline USA v3.3.0
-
- On Feb 21, 1996 18:12:48 in article <HELP! cin.getline to cin>,
- 'scalish@execpc.com (Sherri Scalish)' wrote:
-
-
- >I am a C++ student trying to use the cin.getline and cin stream
- >operators to load in user input. The data I am loading is first
- >character strings, then floats, integers, then back to character
- >strings. Everything works fine with my cin.getlines until after I use
- >the cin to load a integer or float, then nothing happens when I try to
- >use the cin.getline function for the rest of the input. Any ideas to
- >share with me?
- >
- Yes. After having read a value with cin >> somevar, the stream
- is positioned at the newline which you had to enter to complete
- your input. If you now perform a getline(), it will "see" the newline
- and return an empty string (well, not quite return.. but you get
- what I mean). You must first flush this out before you can get
- the stuff on the next line.
-
- There may be more than one newline so, to be safe, do the
- following:
-
- int n;
- char buffer[80];
- ...
- cin >> n;
- cout << "Enter a string: " << flush;
- do
- cin.getline(buffer, 80);
- while(! buffer[0]);
- .... process the next line
-
- --
- Pete Grant
- Kalevi, Inc.
- Software Engineering & development
-